package com.ruoyi.yljf.controller.AdminController;

import java.util.List;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yljf.domain.BookCategory;
import com.ruoyi.yljf.service.IBookCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 书籍分类Controller
 * 
 * @author metaee
 * @date 2025-02-12
 */
@RestController
@RequestMapping("/yljf/category")
public class BookCategoryController extends BaseController {
    @Autowired
    private IBookCategoryService bookCategoryService;

    /**
     * 查询书籍分类列表
     */
    @PreAuthorize("@ss.hasPermi('yljf:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(BookCategory bookCategory) {
        startPage();
        List<BookCategory> list = bookCategoryService.selectBookCategoryList(bookCategory);
        return getDataTable(list);
    }

    /**
     * 导出书籍分类列表
     */
    @PreAuthorize("@ss.hasPermi('yljf:category:export')")
    @Log(title = "书籍分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BookCategory bookCategory) {
        List<BookCategory> list = bookCategoryService.selectBookCategoryList(bookCategory);
        ExcelUtil<BookCategory> util = new ExcelUtil<BookCategory>(BookCategory.class);
        util.exportExcel(response, list, "书籍分类数据");
    }

    /**
     * 获取书籍分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('yljf:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return success(bookCategoryService.getById(categoryId));
    }

    /**
     * 新增书籍分类
     */
    @PreAuthorize("@ss.hasPermi('yljf:category:add')")
    @Log(title = "书籍分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BookCategory bookCategory) {
        return toAjax(bookCategoryService.save(bookCategory));
    }

    /**
     * 修改书籍分类
     */
    @PreAuthorize("@ss.hasPermi('yljf:category:edit')")
    @Log(title = "书籍分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BookCategory bookCategory) {
        return toAjax(bookCategoryService.updateById(bookCategory));
    }

    /**
     * 删除书籍分类
     */
    @PreAuthorize("@ss.hasPermi('yljf:category:remove')")
    @Log(title = "书籍分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(bookCategoryService.removeByIds(Arrays.asList(categoryIds)));
    }
}
